package cn.tta.controller.user;

import cn.tta.common.constant.JwtClaimsConstant;
import cn.tta.common.properties.JwtProperties;
import cn.tta.common.result.Result;
import cn.tta.common.utils.JwtUtil;
import cn.tta.pojo.dto.UserLoginDTO;
import cn.tta.pojo.entity.User;
import cn.tta.pojo.vo.UserLoginVO;
import cn.tta.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/user/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;


    /**
     * 微信登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("微信用户登录：{}",userLoginDTO);
        User user = userService.wxLogin(userLoginDTO);
        HashMap<String, Object> map = new HashMap<>();
        map.put(JwtClaimsConstant.USER_ID,user.getId());
        String jwt = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), map);

        UserLoginVO build = UserLoginVO.builder().id(user.getId()).token(jwt).openid(user.getOpenid()).build();
        return Result.success(build);
    }

}
